home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Testing & Debugging / General tools / Audit app & dcmd / dcmds / C Samples / File.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  5.4 KB  |  250 lines  |  [TEXT/MPS ]

  1. /*     file.c
  2.     This is the FCB dcmd.
  3.  
  4.     Copyright © 1988 Apple Computer, Inc.  All rights reserved.
  5.  
  6.     Modification history:
  7.         29Nov88 sad        revised for new dcmd names.
  8.          5Oct88    sad        broke out formatting routines to put.c
  9.         30Sep88 sad        written
  10.  
  11.     The following MPW commands will build the dcmd and copy it to the
  12.     "Debugger Prefs" file in the System folder. The dcmd's name in
  13.     MacsBug will be the name of the file built by the Linker.
  14.     You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  15.     C Samples folder into this folder.
  16.  
  17.     C Put.c
  18.     C File.c
  19.     Link dcmdGlue.a.o File.c.o put.c.o DRuntime.o "{Libraries}"Interface.o -o File
  20.     BuildDcmd File 1003
  21.     Echo 'include "File";'    |    Rez -a -o "{systemFolder}Debugger Prefs"
  22. */
  23.  
  24. #include <Types.h>
  25. #include <Traps.h>
  26. #include <Memory.h>
  27. #include <Files.h>
  28.  
  29. #include "dcmd.h"
  30. #include "put.h"
  31.  
  32. #define FSFCBLen 0x3f6
  33. #define FCBsPtr 0x34e
  34.  
  35. typedef struct FCB
  36.     {
  37.     unsigned long fcbFlNum;
  38.     unsigned char fcbMdRByt;
  39.     unsigned char fcbTypByt;
  40.     unsigned short fcbSBlk;
  41.     unsigned long fcbEOF;
  42.     unsigned long fcbPLen;
  43.     unsigned long fcbCrPs;
  44.     VCB* fcbVPtr;
  45.     void* fcbBfAdr;
  46.     unsigned short fcbFlPos;
  47.     unsigned long fcbClmpSize;
  48.     void* fcbBTCBPtr;
  49.     unsigned long fcbExtRec[3];
  50.     OSType fcbFType;
  51.     unsigned long fcbCatPos;
  52.     unsigned long fcbDirID;
  53.     char fcbCName[32];
  54.     } FCB;
  55.  
  56.  
  57. static void DrawHdr()
  58. {
  59. //                             1         2         3         4         5         6         7
  60. //                    1234567890123456789012345678901234567890123456789012345678901234567890
  61.     dcmdDrawLine("\pfRef File         Vol      Type Fl Fork    LEof    Mark  FlNum Parent FCB at");
  62. }
  63.  
  64.  
  65. static void DrawFCB(int fref, FCB* fcbp)
  66. {
  67.     PutUHexWord(fref);
  68.     PutSpace();
  69.     PutPStrTruncTo(fcbp->fcbCName,17);
  70.     PutSpace();
  71.     PutPStrTruncTo(fcbp->fcbVPtr->vcbVN,26);
  72.     PutSpace();
  73.     PutOSType(fcbp->fcbFType);
  74.     PutSpace();
  75.     PutChar((fcbp->fcbMdRByt & 0x80) ? 'D' : 'd');
  76.     PutChar((fcbp->fcbMdRByt & 0x01) ? 'W' : 'w');
  77.     PutSpace();
  78.     if (fcbp->fcbMdRByt & 0x02) PutPStr("\prsrc ");
  79.     else PutPStr("\pdata ");
  80.     PutUDecTo(fcbp->fcbEOF,47);
  81.     PutSpace();
  82.     PutUDecTo(fcbp->fcbCrPs,55);
  83.     PutSpace();
  84.     PutUHexZTo(fcbp->fcbFlNum,6,62);
  85.     PutSpace();
  86.     PutUHexZTo(fcbp->fcbDirID,6,69);
  87.     PutSpace();
  88.     PutUHexZTo((unsigned long)fcbp,6,76);
  89.     PutLine();
  90. } // DrawFCB
  91.  
  92.  
  93. static Boolean PrefixPStr(const Str255 astr, const Str255 bstr)
  94. // returns true if astr is equal to a prefix of bstr
  95. //    astr must not be longer than 31 characters
  96. {
  97.     char newstr[31];
  98.     int alen = *astr;
  99.     int blen = *bstr;
  100.     if (alen <= blen)
  101.         {
  102.         BlockMove(bstr+1,newstr+1,alen);
  103.         newstr[0] = alen;
  104.         return EqualString(astr,newstr,false,true);
  105.         }
  106.     else return false;    
  107. } // PrefixPStr
  108.  
  109. // EJECT
  110.  
  111. pascal void CommandEntry(dcmdBlock* paramPtr)
  112. {
  113.     switch (paramPtr->request)
  114.         {
  115.         case dcmdInit:
  116.             break;
  117.  
  118.         case dcmdHelp:
  119.             dcmdDrawLine("\pfile [fRefNum|\"file name\"]");
  120.             dcmdDrawLine("\p   Displays file information for the given fRefNum, file name or all open files.");
  121.             dcmdDrawLine("\p      Flags are D/d=Dirty, W/w=writeable.");
  122.             break;
  123.  
  124.         case dcmdDoIt:
  125.             {
  126.             Boolean doOneFCB = false;
  127.             long fref;
  128.             short c;
  129.             Boolean haveFileName = false;
  130.             Str255 filename;
  131.             int fcblen;                // the length of one fcb
  132.             char* fcbsbase;
  133.             int fcbslen;            // the length of the fcbs block
  134.  
  135.             dcmdSwapWorlds();
  136.  
  137.             dcmdDrawLine("\pDisplaying File Control Blocks");
  138.  
  139.           // get low-memory values after dcmdSwapWorlds()
  140.             fcblen = *(unsigned short*)FSFCBLen;
  141.             fcbsbase = *(char**)FCBsPtr;
  142.             fcbslen = *(unsigned short*)fcbsbase;
  143.  
  144.             if (fcblen != sizeof(FCB))
  145.                 {
  146.                 PutPStr("\FSFCBLen = ");
  147.                 PutUDec(fcblen);
  148.                 PutPStr("\p expected ");
  149.                 PutUDec(fcblen);
  150.                 PutLine(sizeof(FCB));
  151.                 }
  152.             if ((fcbslen - 2) % fcblen != 0)
  153.                 {
  154.                 PutPStr("\pbad fcbslen ");
  155.                 PutUHexWord(fcbslen);
  156.                 PutLine();
  157.                 }
  158.  
  159.             c = dcmdPeekAtNextChar();
  160.             if (c == '"' || c == '\'')
  161.                 {
  162.                 haveFileName = true;
  163.                 (void)dcmdGetNextParameter(filename);
  164.                 }
  165.             else (void)dcmdGetNextExpression(&fref, &doOneFCB);
  166.  
  167.             if (doOneFCB)
  168.                 {
  169.                 fref = (unsigned short)fref;
  170.                 if ((fref > fcbslen) || ((fref - 2) % fcblen != 0))
  171.                     {
  172.                     PutPStr("\pbad file refnum ");
  173.                     PutUHexWord(fref);
  174.                     PutLine();
  175.                     }
  176.                 else
  177.                     {
  178.                     FCB* fcbp = (FCB*)(fref + fcbsbase);
  179.                     if (fcbp->fcbFlNum)
  180.                         {
  181.                         DrawHdr();
  182.                         DrawFCB(fref,fcbp);
  183.                         }
  184.                     else
  185.                         {
  186.                         PutPStr("\pFCB ");
  187.                         PutUHexWord(fref);
  188.                         PutPStr("\p is not in use");
  189.                         PutLine();
  190.                         }
  191.                     }
  192.                 }
  193.             else
  194.                 {
  195.                 int numfcbs = (fcbslen - 2) / fcblen;
  196.                 int fcbsused = 0;
  197.                 Boolean foundOne = false;
  198.                 for (fref = 2;
  199.                      fref < fcbslen;
  200.                      fref += fcblen)
  201.                     {
  202.                     FCB* fcbp = (FCB*)(fcbsbase + fref);
  203.                     if (fcbp->fcbFlNum &&
  204.                         (!haveFileName || PrefixPStr(filename,fcbp->fcbCName)))
  205.                         {
  206.                         fcbsused++;
  207.                         if (!foundOne)
  208.                             {
  209.                             DrawHdr();
  210.                             foundOne = true;
  211.                             }
  212.                         DrawFCB(fref,fcbp);
  213.                         }
  214.                     if (paramPtr->aborted) break;
  215.                     }
  216.                 if (!paramPtr->aborted)
  217.                     if (haveFileName)
  218.                         {
  219.                         if (!foundOne)
  220.                             {
  221.                             PutPStr("\pno open files match \"");
  222.                             PutPStr(filename);
  223.                             PutChar('"');
  224.                             PutLine();
  225.                             }
  226.                         }
  227.                     else
  228.                         {
  229.                         PutUDec(numfcbs);
  230.                         PutPStr("\p FCBs, ");
  231.                         PutUDec(fcbsused);
  232.                         PutPStr("\p in use, ");
  233.                         PutUDec(numfcbs - fcbsused);
  234.                         PutPStr("\p free");
  235.                         PutLine();
  236.                         }
  237.                 }    
  238.  
  239.             dcmdSwapWorlds();
  240.             }
  241.             break;
  242.  
  243.         default:
  244.             PutPStr("\punknown request ");
  245.             PutUDec(paramPtr->request);
  246.             PutLine();
  247.             break;
  248.         }
  249. } // CommandEntry
  250.